{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "\"\"\"A very simple MNIST classifier.\n",
    "See extensive documentation at\n",
    "https://www.tensorflow.org/get_started/mnist/beginners\n",
    "\"\"\"\n",
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "#可视化\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "FLAGS = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们在这里调用系统提供的Mnist数据函数为我们读入数据，如果没有下载的话则进行下载。\n",
    "\n",
    "<font color=#ff0000>**这里将data_dir改为适合你的运行环境的目录**</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-2-1f2e3e29ecda>:3: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting Mnist_data/train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting Mnist_data/train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting Mnist_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting Mnist_data/t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From C:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "(55000, 784) (55000, 10)\n"
     ]
    }
   ],
   "source": [
    "# Import data\n",
    "data_dir = 'Mnist_data/'\n",
    "mnist = input_data.read_data_sets(data_dir, one_hot=True)\n",
    "print(mnist.train.images.shape,mnist.train.labels.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义各层神经元数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_input_layer = 28 * 28  # 输入层\n",
    "n_output_layer = 10  # 输出层\n",
    "\n",
    "# 层数的选择：线性数据使用1层，非线性数据使用2层, 超级非线性使用3+层。层数、神经元过多会导致过拟合\n",
    "n_layer_1 = 500  # hide layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义3隐层神经网络\n",
    "def neural_network(x):\n",
    "    # 定义第一层\"神经元\"的权重和biases\n",
    "    layer_1_w_b = {'w_': tf.Variable(tf.truncated_normal([n_input_layer, n_layer_1])),\n",
    "                   'b_': tf.Variable(tf.constant(0.1, shape=[n_layer_1]))}\n",
    "    # 定义输出层\"神经元\"的权重和biases\n",
    "    layer_output_w_b = {'w_': tf.Variable(tf.truncated_normal([n_layer_1, n_output_layer])),\n",
    "                        'b_': tf.Variable(tf.constant(0.1, shape=[n_output_layer]))}\n",
    " \n",
    "    # w·x+b\n",
    "    layer_1 = tf.add(tf.matmul(x, layer_1_w_b['w_']), layer_1_w_b['b_'])\n",
    "    layer_1 = tf.nn.relu(layer_1)  # 激活函数 relu sigmoid  tanh elu  softplus\n",
    "    layer_output = tf.add(tf.matmul(layer_1, layer_output_w_b['w_']), layer_output_w_b['b_'])\n",
    " \n",
    "    return layer_output, layer_1_w_b, layer_output_w_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 每次使用100条数据进行训练\n",
    "batch_size = 100\n",
    "\n",
    "x = tf.placeholder(dtype=tf.float32, shape=[None, n_input_layer])   # 输入数据占位符\n",
    "y = tf.placeholder(dtype=tf.float32, shape=[None, n_output_layer])   # 输出数据占位符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用数据训练神经网络\n",
    "def train_neural_network(X, Y, epochs=10):\n",
    "    #创建神经网络\n",
    "    y, layer_1_w_b, layer_output_w_b = neural_network(X)\n",
    "    #代价函数\n",
    "    cost_func = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(Y,1), logits= y))\n",
    "    #惩罚因子\n",
    "    regularization_rate = 0.0001\n",
    "    #计算L2正则化损失函数\n",
    "    regularizer = tf.contrib.layers.l2_regularizer(regularization_rate)\n",
    "    #将变量的L2正则化损失添加到集合中\n",
    "    regularization = regularizer(layer_1_w_b['w_']) +  regularizer(layer_output_w_b['w_'])\n",
    "    \n",
    "    #获取整个模型的损失函数,tf.get_collection(\"losses\")返回集合中定义的损失\n",
    "    #将整个集合中的损失相加得到整个模型的损失函数\n",
    "    loss = cost_func + regularization\n",
    "\n",
    "    #学习速率，随迭代次数进行递减\n",
    "    global_step = tf.Variable(0, trainable=False)\n",
    "    #设置基础学习率\n",
    "    starter_learning_rate = 0.8\n",
    "    #设置学习率的衰减率\n",
    "    learning_rate_decay = 0.99\n",
    "    #设置指数衰减学习率\n",
    "    learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, mnist.train.num_examples/batch_size, learning_rate_decay)\n",
    "    \n",
    "    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)  \n",
    "    \n",
    "    #每迭代一次需要更新神经网络中的参数\n",
    "    train_op = tf.group(train_step)\n",
    " \n",
    "    with tf.Session() as session:\n",
    "        session.run(tf.global_variables_initializer())\n",
    "        \n",
    "        correct = tf.equal(tf.argmax(y, 1), tf.argmax(Y, 1))\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))\n",
    "        \n",
    "        epoch_accuracy_mat = []\n",
    "        epoch_accuracy_t_mat = []\n",
    "        #迭代训练\n",
    "        for i in range(30000):\n",
    "            batch_xs,batch_ys = mnist.train.next_batch(batch_size)\n",
    "            session.run(train_op,feed_dict={X: batch_xs, Y: batch_ys, global_step:i})\n",
    "            if i % 1000 == 0 or i % (int(mnist.train.num_examples / batch_size)) == 0:\n",
    "                epoch_accuracy = accuracy.eval({X: mnist.train.images, Y: mnist.train.labels})\n",
    "                epoch_accuracy_t = accuracy.eval({X: mnist.test.images, Y: mnist.test.labels})\n",
    "                print('i ', i, ' accuracy  : ', epoch_accuracy)\n",
    "                print('i ', i, ' accuracy_t: ', epoch_accuracy_t)\n",
    "                epoch_accuracy_mat.append(epoch_accuracy)\n",
    "                epoch_accuracy_t_mat.append(epoch_accuracy_t)\n",
    " \n",
    "        \n",
    "        print('训练准确率: ', accuracy.eval({X: mnist.train.images, Y: mnist.train.labels}))\n",
    "        print('测试准确率: ', accuracy.eval({X: mnist.test.images, Y: mnist.test.labels}))\n",
    "        \n",
    "        plt.plot(range(epochs), epoch_loss_mat) \n",
    "        plt.xlabel('epochs')\n",
    "        plt.ylabel('epoch_loss')\n",
    "        plt.show() \n",
    "        \n",
    "        plt.plot(range(epochs), epoch_accuracy_mat, label='Train Accuracy') \n",
    "        plt.plot(range(epochs), epoch_accuracy_t_mat, label='Test Accuracy')\n",
    "        plt.legend()\n",
    "        plt.xlabel('epochs')\n",
    "        plt.ylabel('accuracy')\n",
    "        plt.show() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i  0  accuracy  :  0.27603635\n",
      "i  0  accuracy_t:  0.2868\n",
      "i  550  accuracy  :  0.9107636\n",
      "i  550  accuracy_t:  0.9026\n",
      "i  1000  accuracy  :  0.93209094\n",
      "i  1000  accuracy_t:  0.9146\n",
      "i  1100  accuracy  :  0.93767273\n",
      "i  1100  accuracy_t:  0.9213\n",
      "i  1650  accuracy  :  0.9497091\n",
      "i  1650  accuracy_t:  0.9299\n",
      "i  2000  accuracy  :  0.95974547\n",
      "i  2000  accuracy_t:  0.9374\n",
      "i  2200  accuracy  :  0.96445453\n",
      "i  2200  accuracy_t:  0.9412\n",
      "i  2750  accuracy  :  0.96976364\n",
      "i  2750  accuracy_t:  0.9451\n",
      "i  3000  accuracy  :  0.97416365\n",
      "i  3000  accuracy_t:  0.9475\n",
      "i  3300  accuracy  :  0.97532725\n",
      "i  3300  accuracy_t:  0.9484\n",
      "i  3850  accuracy  :  0.9790909\n",
      "i  3850  accuracy_t:  0.9515\n",
      "i  4000  accuracy  :  0.9801818\n",
      "i  4000  accuracy_t:  0.9522\n",
      "i  4400  accuracy  :  0.98210907\n",
      "i  4400  accuracy_t:  0.9528\n",
      "i  4950  accuracy  :  0.9857091\n",
      "i  4950  accuracy_t:  0.9555\n",
      "i  5000  accuracy  :  0.98547274\n",
      "i  5000  accuracy_t:  0.9556\n",
      "i  5500  accuracy  :  0.98710907\n",
      "i  5500  accuracy_t:  0.9562\n",
      "i  6000  accuracy  :  0.9891091\n",
      "i  6000  accuracy_t:  0.959\n",
      "i  6050  accuracy  :  0.9876182\n",
      "i  6050  accuracy_t:  0.957\n",
      "i  6600  accuracy  :  0.99\n",
      "i  6600  accuracy_t:  0.9607\n",
      "i  7000  accuracy  :  0.99163634\n",
      "i  7000  accuracy_t:  0.9621\n",
      "i  7150  accuracy  :  0.9912909\n",
      "i  7150  accuracy_t:  0.9608\n",
      "i  7700  accuracy  :  0.99276364\n",
      "i  7700  accuracy_t:  0.9616\n",
      "i  8000  accuracy  :  0.99294543\n",
      "i  8000  accuracy_t:  0.9633\n",
      "i  8250  accuracy  :  0.9934545\n",
      "i  8250  accuracy_t:  0.9641\n",
      "i  8800  accuracy  :  0.99430907\n",
      "i  8800  accuracy_t:  0.9639\n",
      "i  9000  accuracy  :  0.99465454\n",
      "i  9000  accuracy_t:  0.963\n",
      "i  9350  accuracy  :  0.9954\n",
      "i  9350  accuracy_t:  0.9648\n",
      "i  9900  accuracy  :  0.9961454\n",
      "i  9900  accuracy_t:  0.9659\n",
      "i  10000  accuracy  :  0.9958182\n",
      "i  10000  accuracy_t:  0.9653\n",
      "i  10450  accuracy  :  0.9954909\n",
      "i  10450  accuracy_t:  0.9658\n",
      "i  11000  accuracy  :  0.99685454\n",
      "i  11000  accuracy_t:  0.9684\n",
      "i  11550  accuracy  :  0.99707276\n",
      "i  11550  accuracy_t:  0.967\n",
      "i  12000  accuracy  :  0.9973091\n",
      "i  12000  accuracy_t:  0.9674\n",
      "i  12100  accuracy  :  0.9977091\n",
      "i  12100  accuracy_t:  0.9682\n",
      "i  12650  accuracy  :  0.99798185\n",
      "i  12650  accuracy_t:  0.969\n",
      "i  13000  accuracy  :  0.9983091\n",
      "i  13000  accuracy_t:  0.9696\n",
      "i  13200  accuracy  :  0.9982909\n",
      "i  13200  accuracy_t:  0.9701\n",
      "i  13750  accuracy  :  0.99847275\n",
      "i  13750  accuracy_t:  0.9712\n",
      "i  14000  accuracy  :  0.99865454\n",
      "i  14000  accuracy_t:  0.9723\n",
      "i  14300  accuracy  :  0.99881816\n",
      "i  14300  accuracy_t:  0.9706\n",
      "i  14850  accuracy  :  0.99881816\n",
      "i  14850  accuracy_t:  0.9712\n",
      "i  15000  accuracy  :  0.9987091\n",
      "i  15000  accuracy_t:  0.971\n",
      "i  15400  accuracy  :  0.9989455\n",
      "i  15400  accuracy_t:  0.9722\n",
      "i  15950  accuracy  :  0.999\n",
      "i  15950  accuracy_t:  0.9723\n",
      "i  16000  accuracy  :  0.9991818\n",
      "i  16000  accuracy_t:  0.9729\n",
      "i  16500  accuracy  :  0.9990909\n",
      "i  16500  accuracy_t:  0.9732\n",
      "i  17000  accuracy  :  0.99938184\n",
      "i  17000  accuracy_t:  0.9736\n",
      "i  17050  accuracy  :  0.9993273\n",
      "i  17050  accuracy_t:  0.9728\n",
      "i  17600  accuracy  :  0.9992545\n",
      "i  17600  accuracy_t:  0.9729\n",
      "i  18000  accuracy  :  0.9994\n",
      "i  18000  accuracy_t:  0.9749\n",
      "i  18150  accuracy  :  0.99930906\n",
      "i  18150  accuracy_t:  0.9734\n",
      "i  18700  accuracy  :  0.99956363\n",
      "i  18700  accuracy_t:  0.9745\n",
      "i  19000  accuracy  :  0.9995818\n",
      "i  19000  accuracy_t:  0.9739\n",
      "i  19250  accuracy  :  0.9995273\n",
      "i  19250  accuracy_t:  0.9745\n",
      "i  19800  accuracy  :  0.99954545\n",
      "i  19800  accuracy_t:  0.9751\n",
      "i  20000  accuracy  :  0.9996182\n",
      "i  20000  accuracy_t:  0.9744\n",
      "i  20350  accuracy  :  0.9996727\n",
      "i  20350  accuracy_t:  0.9753\n",
      "i  20900  accuracy  :  0.9996\n",
      "i  20900  accuracy_t:  0.9756\n",
      "i  21000  accuracy  :  0.9996182\n",
      "i  21000  accuracy_t:  0.9755\n",
      "i  21450  accuracy  :  0.9997454\n",
      "i  21450  accuracy_t:  0.9746\n",
      "i  22000  accuracy  :  0.9996909\n",
      "i  22000  accuracy_t:  0.9755\n",
      "i  22550  accuracy  :  0.99972725\n",
      "i  22550  accuracy_t:  0.9764\n",
      "i  23000  accuracy  :  0.9998364\n",
      "i  23000  accuracy_t:  0.9762\n",
      "i  23100  accuracy  :  0.99965453\n",
      "i  23100  accuracy_t:  0.9758\n",
      "i  23650  accuracy  :  0.99972725\n",
      "i  23650  accuracy_t:  0.9769\n",
      "i  24000  accuracy  :  0.9997454\n",
      "i  24000  accuracy_t:  0.9768\n",
      "i  24200  accuracy  :  0.9998364\n",
      "i  24200  accuracy_t:  0.9759\n",
      "i  24750  accuracy  :  0.99987274\n",
      "i  24750  accuracy_t:  0.9768\n",
      "i  25000  accuracy  :  0.9998182\n",
      "i  25000  accuracy_t:  0.9777\n",
      "i  25300  accuracy  :  0.99985456\n",
      "i  25300  accuracy_t:  0.9771\n",
      "i  25850  accuracy  :  0.9998364\n",
      "i  25850  accuracy_t:  0.9775\n",
      "i  26000  accuracy  :  0.99987274\n",
      "i  26000  accuracy_t:  0.9781\n",
      "i  26400  accuracy  :  0.99987274\n",
      "i  26400  accuracy_t:  0.9776\n",
      "i  26950  accuracy  :  0.9998182\n",
      "i  26950  accuracy_t:  0.9783\n",
      "i  27000  accuracy  :  0.99985456\n",
      "i  27000  accuracy_t:  0.9788\n",
      "i  27500  accuracy  :  0.99985456\n",
      "i  27500  accuracy_t:  0.978\n",
      "i  28000  accuracy  :  0.9999273\n",
      "i  28000  accuracy_t:  0.9784\n",
      "i  28050  accuracy  :  0.9998909\n",
      "i  28050  accuracy_t:  0.9778\n",
      "i  28600  accuracy  :  0.99994546\n",
      "i  28600  accuracy_t:  0.9785\n",
      "i  29000  accuracy  :  0.99987274\n",
      "i  29000  accuracy_t:  0.9787\n",
      "i  29150  accuracy  :  0.99985456\n",
      "i  29150  accuracy_t:  0.9777\n",
      "i  29700  accuracy  :  0.9999091\n",
      "i  29700  accuracy_t:  0.9784\n",
      "训练准确率:  0.99985456\n",
      "测试准确率:  0.9786\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'epoch_loss_mat' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-989ef88c851e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtrain_neural_network\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mY\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mepochs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m15\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-6-24010a39e74d>\u001b[0m in \u001b[0;36mtrain_neural_network\u001b[1;34m(X, Y, epochs)\u001b[0m\n\u001b[0;32m     54\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'测试准确率: '\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maccuracy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meval\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m{\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mmnist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtest\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimages\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mY\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mmnist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtest\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlabels\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     55\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 56\u001b[1;33m         \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mepochs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mepoch_loss_mat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     57\u001b[0m         \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mxlabel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'epochs'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     58\u001b[0m         \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mylabel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'epoch_loss'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'epoch_loss_mat' is not defined"
     ]
    }
   ],
   "source": [
    "train_neural_network(X=x, Y=y, epochs=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "毫无疑问，这个模型是一个非常简陋，性能也不理想的模型。目前只能达到92%左右的准确率。\n",
    "接下来，希望大家利用现有的知识，将这个模型优化至98%以上的准确率。\n",
    "Hint：\n",
    "- 多隐层\n",
    "- 激活函数\n",
    "- 正则化\n",
    "- 初始化\n",
    "- 摸索一下各个超参数\n",
    "  - 隐层神经元数量\n",
    "  - 学习率\n",
    "  - 正则化惩罚因子\n",
    "  - 最好每隔几个step就对loss、accuracy等等进行一次输出，这样才能有根据地进行调整"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
